package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CategoryException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {


    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    /**
     * 根据id删除分类
     * @param id
     */
    public void deleteById(Long id){
        //1 菜品存在
        Integer count1 = dishService.lambdaQuery().eq(Dish::getCategoryId, id).count();
        if(count1!=0){
            throw new CategoryException("分类中有包含菜品,不能删除");
        }
        //2 套餐存在
        Integer count2 = setmealService.lambdaQuery().eq(Setmeal::getCategoryId, id).count();
        if(count2!=0){
            throw new CategoryException("分类中包含套餐，不能删除");
        }
        //3 删除
        removeById(id);


    }

}
